#include "GameRoot.h"
#include "common/CsvParser.h"
#include "SimpleAudioEngine.h"
#include "common/common.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include <android/log.h>
#endif

SceneStart* GameRoot::m_pSceneStart = NULL;
SceneGame* GameRoot::m_pSceneGame = NULL;
SceneSetting* GameRoot::m_pSceneSetting = NULL;
SceneLevel* GameRoot::m_pSceneLevel=NULL;
DataManager* GameRoot::m_pDataManager=NULL;
int GameRoot::m_nAdNum=0;
GameRoot::GameRoot(void)
{
}


GameRoot::~GameRoot(void)
{
}


void GameRoot::InitializeResource()
{
	CCSpriteFrameCache* cache = 
		CCSpriteFrameCache::sharedSpriteFrameCache();
	cache->addSpriteFramesWithFile("beatball_new.plist", "beatball_new.png");

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_CLICK);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_MATCH);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_RESTART);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_EXCELLENT);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_DONE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(MUSIC_WRONG);
	
}
SceneStart* GameRoot::getSceneStart()
{
	if (m_pSceneStart == NULL)
	{
		m_pSceneStart = new SceneStart();
	}
	return m_pSceneStart;
}


SceneGame* GameRoot::getSceneGame()
{
	if (NULL == m_pSceneGame)
	{
		m_pSceneGame=new SceneGame();
	}
	return m_pSceneGame;
}

SceneSetting* GameRoot::getSceneSetting()
{
	if (NULL == m_pSceneSetting)
	{
		m_pSceneSetting=new SceneSetting();
	}
	return m_pSceneSetting;
}

DataManager* GameRoot::getDataManager()
{
	if (NULL == m_pDataManager)
	{
		m_pDataManager = new DataManager();
	}
	return m_pDataManager;
}

void GameRoot::ShowAd()
{
	static int nCount =0;
	if (nCount%2 == 0)
	{
		ShowMobiAd();
	}
	else
	{
		ShowYoumiAd();
	}
	nCount++;
}

void GameRoot::ShowYoumiAd()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo minfo; 
	jobject jobj;  
	bool b = JniHelper::getStaticMethodInfo(minfo,
		"com/app_wu/beatbean/beatbean",
		"ShowAd",
		"()V");
	if (b)
	{
		jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
	}
	else
	{
		CCLog("Error to call ShowAd");
	}

#endif
}

void GameRoot::ShowMobiAd()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo minfo; 
	jobject jobj;  
	bool b = JniHelper::getStaticMethodInfo(minfo,
		"com/app_wu/beatbean/beatbean",
		"ShowYoumiAd",
		"()V");
	if (b)
	{
		jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
	}
	else
	{
		CCLog("Error to call ShowAd");
	}

#endif

}

SceneLevel* GameRoot::getSceneLevel()
{
	if (NULL == m_pSceneLevel)
	{
		m_pSceneLevel = new SceneLevel();
	}
	return m_pSceneLevel;
}



